home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / uupc11ys.zip / LIB / USERTABL.C < prev    next >
C/C++ Source or Header  |  1993-04-15  |  13KB  |  334 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       u s e r t a b l . c                                          */
  3. /*                                                                    */
  4. /*       User table routines for UUPC/extended                        */
  5. /*                                                                    */
  6. /*       Copyright (C) 1989, 1990 by Andrew H. Derbyshire             */
  7. /*                                                                    */
  8. /*       See file README.SCR for restrictions on re-distribution.     */
  9. /*                                                                    */
  10. /*       History:                                                     */
  11. /*          Create from hostable.c                                    */
  12. /*--------------------------------------------------------------------*/
  13.  
  14. /*
  15.  *    $Id: USERTABL.C 1.4 1993/04/15 03:17:21 ahd Exp $
  16.  *
  17.  *    $Log: USERTABL.C $
  18.  *     Revision 1.4  1993/04/15  03:17:21  ahd
  19.  *     Use standard define for undefined user names
  20.  *
  21.  *     Revision 1.3  1993/04/11  00:31:04  ahd
  22.  *     Global edits for year, TEXT, etc.
  23.  *
  24.  * Revision 1.2  1992/11/22  20:58:55  ahd
  25.  * Use strpool to allocate const strings
  26.  * Normalize directories as read
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <ctype.h>
  35. #include <sys/types.h>
  36.  
  37. #include "lib.h"
  38. #include "hlib.h"
  39. #include "expath.h"
  40. #include "usertabl.h"
  41. #include "hostable.h"
  42. #include "security.h"
  43.  
  44. #define MAXUSERS  100         /* max number of unique users in PASSWD */
  45.  
  46. struct UserTable *users = NULL;  /* Public to allow router.c to use it  */
  47.  
  48. size_t  UserElements = 0;        /* Public to allow router.c to use it  */
  49.  
  50. static size_t loaduser( void );
  51.  
  52. static int usercmp( const void *a , const void *b );
  53.  
  54. char *NextField( char *input );
  55.  
  56. static char uucpsh[] = UUCPSHELL;
  57.  
  58. currentfile();
  59.  
  60. /*--------------------------------------------------------------------*/
  61. /*    c h e c k u s e r                                               */
  62. /*                                                                    */
  63. /*    Look up a user name in the PASSWD file                          */
  64. /*--------------------------------------------------------------------*/
  65.  
  66. struct UserTable *checkuser(const char *name)
  67. {
  68.    int   lower;
  69.    int   upper;
  70.  
  71.    if ( (name == NULL) || (strlen(name) == 0) )
  72.    {
  73.       printmsg(0,"checkuser: Invalid argument!");
  74.       panic();
  75.    }
  76.  
  77.    printmsg(14,"checkuser: Searching for user id %s",name);
  78.  
  79.  /*------------------------------------------------------------------*/
  80.  /*             Initialize the host name table if needed             */
  81.  /*------------------------------------------------------------------*/
  82.  
  83.    if (UserElements == 0)           /* host table initialized yet?   */
  84.       UserElements = loaduser();        /* No --> load it            */
  85.  
  86.    lower = 0;
  87.    upper = UserElements - 1;
  88.  
  89. /*--------------------------------------------------------------------*/
  90. /*              Peform a binary search on the user table              */
  91. /*--------------------------------------------------------------------*/
  92.  
  93.    while ( lower <= upper )
  94.    {
  95.       int midpoint;
  96.       int hit;
  97.       midpoint = (lower + upper) / 2;
  98.  
  99.       hit = stricmp(name,users[midpoint].uid);
  100.  
  101.       if (hit > 0)
  102.          lower = midpoint + 1;
  103.       else if (hit < 0)
  104.          upper = midpoint - 1;
  105.       else
  106.          return &users[midpoint];
  107.    }
  108.  
  109. /*--------------------------------------------------------------------*/
  110. /*         We didn't find the user.  Return failure to caller         */
  111. /*--------------------------------------------------------------------*/
  112.  
  113.    return BADUSER;
  114.  
  115. }  /* checkuser */
  116.  
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*    i n i t u s e r                                                 */
  120. /*                                                                    */
  121. /*    Intializes a user table entry for for loaduser                  */
  122. /*--------------------------------------------------------------------*/
  123.  
  124. struct UserTable *inituser(char *name)
  125. {
  126.  
  127.    size_t hit = UserElements;
  128.    size_t element = 0;
  129.  
  130.    if (users == NULL)
  131.    {
  132.       users = calloc(MAXUSERS, sizeof(*users));
  133.       checkref(users);
  134.    }
  135.  
  136. /*--------------------------------------------------------------------*/
  137. /*    Add the user to the table.  Note that we must add the user      */
  138. /*    to the table ourselves (rather than use lsearch) because we     */
  139. /*    must make a copy of the string; the *token we use for the       */
  140. /*    search is in the middle of our I/O buffer!                      */
  141. /*--------------------------------------------------------------------*/
  142.  
  143.    while ( element < hit )
  144.    {
  145.       if (equali( users[element].uid , name ))
  146.          hit = element;
  147.       else
  148.          element++;
  149.    }
  150.  
  151.    if (hit == UserElements)
  152.    {
  153.       users[hit].uid      = newstr(name);
  154.       checkref(users[hit].uid);
  155.       users[hit].realname = EMPTY_GCOS;
  156.       users[hit].beep     = NULL;
  157.       users[hit].homedir  = E_pubdir;
  158.       users[hit].hsecure  = NULL;
  159.       users[hit].password = NULL;
  160.       users[hit].sh       = uucpsh;
  161.       assert(UserElements++ < MAXUSERS);
  162.    } /* if */
  163.  
  164.    return &users[hit];
  165.  
  166. } /* inituser */
  167.  
  168. /*--------------------------------------------------------------------*/
  169. /*    l o a d u s e r                                                 */
  170. /*                                                                    */
  171. /*    Load the user password file                                     */
  172. /*--------------------------------------------------------------------*/
  173.  
  174. static size_t loaduser( void )
  175. {
  176.    char s_systems[FILENAME_MAX];
  177.    FILE *stream;
  178.    struct UserTable *userp;
  179.    size_t hit;
  180.    char buf[BUFSIZ];
  181.    char *token;
  182.  
  183. /*--------------------------------------------------------------------*/
  184. /*     First, load in the active user as first user in the table      */
  185. /*--------------------------------------------------------------------*/
  186.  
  187.    userp = inituser( E_mailbox );
  188.    userp->realname = E_name;
  189.    userp->homedir  = E_homedir;
  190.  
  191. /*--------------------------------------------------------------------*/
  192. /*       Password file format:                                        */
  193. /*          user id:password:::user/system name:homedir:shell         */
  194. /*--------------------------------------------------------------------*/
  195.  
  196.    mkfilename(s_systems, E_confdir, PASSWD);
  197.  
  198.    if ((stream = FOPEN(s_systems, "r",TEXT_MODE)) == NULL)
  199.    {
  200.       printmsg(2,"loaduser: Cannot open password file %s",s_systems);
  201.       users = realloc(users, UserElements *  sizeof(*users));
  202.       checkref(users);
  203.       return UserElements;
  204.    } /* if */
  205.  
  206. /*--------------------------------------------------------------------*/
  207. /*                 The password file is open; read it                 */
  208. /*--------------------------------------------------------------------*/
  209.  
  210.    while (! feof(stream)) {
  211.       if (fgets(buf,BUFSIZ,stream) == NULL)   /* Try to read a line      */
  212.          break;               /* Exit if end of file                 */
  213.       if ((*buf == '#') || (*buf == '\0'))
  214.          continue;            /* Line is a comment; loop again       */
  215.       if ( buf[ strlen(buf) - 1 ] == '\n')
  216.          buf[ strlen(buf) - 1 ] = '\0';
  217.       token = NextField(buf);
  218.       if (token    == NULL)   /* Any data?                           */
  219.          continue;            /* No --> read another line            */
  220.       userp = inituser(token);/* Initialize record for user          */
  221.  
  222.       if (userp->password != NULL)  /* Does the user already exist?  */
  223.       {                       /* Yes --> Report and ignore           */
  224.          printmsg(0,"loaduser: Duplicate entry for '%s' in '%s' ignored",
  225.                token,s_systems);
  226.          continue;            /* System already in /etc/passwd,
  227.                                  ignore it.                          */
  228.       }
  229.  
  230.       token = NextField(NULL);   /* Get the user password            */
  231.       if (!equal(token,"*"))     /* User can login?                  */
  232.          userp->password = newstr(token); /* Yes --> Set password    */
  233.  
  234.       token = NextField(NULL);   /* Use  UNIX user number as tone    */
  235.                                  /* to beep at                       */
  236.       if (token != NULL)
  237.          userp->beep = newstr( token );
  238.  
  239.       token = NextField(NULL);   /* Skip UNIX group number           */
  240.  
  241.       token = NextField(NULL);   /* Get the formal user name         */
  242.       if (token != NULL)         /* Did they provide user name?      */
  243.          userp->realname = newstr(token); /* Yes --> Copy            */
  244.  
  245.       token = NextField(NULL);   /* Get home directory (optional)    */
  246.       if ( token != NULL)
  247.          userp->homedir = newstr(normalize( token ));
  248.  
  249.       token = NextField(NULL);   /* Get user shell (optional)        */
  250.       if ( token != NULL )       /* Did we get it?                   */
  251.          userp->sh = newstr(token); /* Yes --> Copy it in            */
  252.  
  253.    }  /* while */
  254.  
  255.    fclose(stream);
  256.    users = realloc(users, UserElements *  sizeof(*users));
  257.    checkref(users);
  258.  
  259.    qsort(users, UserElements ,sizeof(users[0]) , usercmp);
  260.  
  261.    for (hit = 0 ; hit < UserElements; hit ++)
  262.    {
  263.       printmsg(8,"loaduser: user[%d] user id(%s) no(%s) name(%s) "
  264.                  "home directory(%s) shell(%s)",
  265.          hit,
  266.          users[hit].uid,
  267.          users[hit].beep == NULL ? "NONE" : users[hit].beep,
  268.          users[hit].realname,
  269.          users[hit].homedir,
  270.          users[hit].sh);
  271.    } /* for */
  272.  
  273.    return UserElements;
  274. } /* loaduser */
  275.  
  276. /*--------------------------------------------------------------------*/
  277. /*    u s e r c m p                                                   */
  278. /*                                                                    */
  279. /*    Accepts indirect pointers to two strings and compares           */
  280. /*    them using stricmp (case insensitive string compare)            */
  281. /*--------------------------------------------------------------------*/
  282.  
  283. int usercmp( const void *a , const void *b )
  284. {
  285.    return stricmp(((struct UserTable*) a)->uid,
  286.         ((struct UserTable*) b)->uid);
  287. }  /*usercmp*/
  288.  
  289. /*--------------------------------------------------------------------*/
  290. /*    n e x t f i e l d                                               */
  291. /*                                                                    */
  292. /*    Find the next field in the user password file                   */
  293. /*--------------------------------------------------------------------*/
  294.  
  295. char *NextField( char *input )
  296. {
  297.    static char *start = NULL;
  298.    char *next = NULL;
  299.  
  300.    if (input == NULL)         /* Starting a new field?               */
  301.    {
  302.       if ( start  == NULL )   /* Anything left to parse?             */
  303.          return NULL;         /* No --> Return empty string          */
  304.       else
  305.          input = start;       /* Yes --> Continue parse of old one   */
  306.    } /* if */
  307.  
  308. /*--------------------------------------------------------------------*/
  309. /*    Look for the next field; because MS-DOS directories may have    */
  310. /*    a sequence of x:/ or x:\ where 'x' is a drive letter, we take   */
  311. /*    special care to allow DOS directories to appear unmolested      */
  312. /*    in the password file                                            */
  313. /*--------------------------------------------------------------------*/
  314.  
  315.    if ((strlen(input) > 2) && isalpha(input[0]) && (input[1] == ':') &&
  316.        ((input[2] == '/') || (input[2] == '\\')))
  317.       next = strchr( &input[2], ':');   /* Find start of next field  */
  318.    else
  319.       next = strchr( input, ':');   /* Find start of next field      */
  320.  
  321.    if (next == NULL )         /* Last field?                         */
  322.       start = NULL;           /* Yes --> Make next call return NULL  */
  323.    else {                     /* No                                  */
  324.       *next = '\0';           /* Terminate the string                */
  325.       start = ++next;         /* Have next call look at next field   */
  326.    } /* else */
  327.  
  328.    if (strlen(input))         /* Did we get anything in the field?   */
  329.       return input;           /* Yes --> Return the string           */
  330.    else
  331.       return NULL;            /* Field is empty, return NULL         */
  332.  
  333. } /* NextField */
  334.